<template>
  <updata @submit="updateChong" :Chong="updateC"></updata>
</template>
<script>
import updata from "@/components/addChong";
import PetownersApi from "@/services/Petowners";
export default {
  data() {
    return {
      petArr: [],
      rid: ""
    };
  },
  components: {
    updata
  },
  async created() {
    const res = await PetownersApi.getByPetId(this.$route.params.id);
    this.petArr = res.data.pet;
    this.rid = res.data._id;
  },
  computed: {
    updateC() {
      let c = {};
      this.petArr.forEach(e => {
        if (e._id === this.$route.params.id) {
          c = e;
        }
      });
      c.rid = this.rid;
      return c;
    }
  },
  methods: {
    async updateChong(petowner) {
      this.petArr.forEach(e => {
        if (e._id === this.$route.params.id) {
          e.name = petowner.name;
          e.pettype = petowner.pettype;
          e.species = petowner.species;
          e.color = petowner.color;
          e.birth = petowner.birth;
          e.character = petowner.character;
        }
      });
      await PetownersApi.addpet({
        _id: this.rid,
        pet: this.petArr
      });
      this.$message({
        message: "修改成功",
        type: "success",
        onClose: () => {
          this.$router.push({
            name: "查看宠物",
            params: {
              id: this.rid
            }
          });
        }
      });
    }
  }
};
</script>

